<?
/**
  * Youtube PHP Class
  * v.000000003
  * By Harper Reed
  * Harper@nata2.org
  * I am pretty awesome
  * 
  * I hate documenting my code. I cannot lie. But its pretty easy. 
  * I will make more documentation later. Basically you just do this:
  * 
  * $tag= "stupid+videos"; 
  * $youtube = new youtubeService("DEV_ID");
  * $videos = $youtube->getVideos($tag);
  * 
  * This will give you an array of videos. I will be creating some crazy 
  * awesome helper functions to help you on your way. I will also at some 
  * point implement the XMLRPC api. we shall see.  
  *
  * -----------------------------------------------------------
  *
  * Copyright 2006 Harper Reed
  * 
  * -----------------------------------------------------------
  *
  * LICENSE
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License (GPL)
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * To read the license please visit http://www.gnu.org/copyleft/gpl.html
  *
  */

class youtubeService{

/**
  * A youtube dev id - http://youtube.com/my_profile_dev
  * @access private
  * @var integer|string
  */
private $devkey;


/**
  * Constructor - sets up youtube devid
  * @param string $devkey
  */
function youtubeService($devid){
	$this->devkey= $devid;
}

/**
  * Retrieves the public parts of a user profile.
  * @param string $user
  * @return string $results
  */
function users_get_profile($user)
{
	$functionName = "youtube.users.get_profile";
	$payload = array("user"=>$user);
	$results = $this->getAPIResponse($functionName, $payload);
	return $results;
}

/**
  * Lists a user's favorite videos.
  * @param string $user
  * @return string $results
  */
function users_list_favorite_videos($user)
{
	$functionName = "youtube.users.list_favorite_videos";
	$payload = array("user"=>$user);
	$results = $this->getAPIResponse($functionName, $payload);
	return $results;
}

/**
  * Lists a user's friends.
  * @param string $user
  * @return string $results
  */
function users_list_friends($user)
{
	$functionName = "youtube.users.list_friends";
	$payload = array("user"=>$user);
	$results = $this->getAPIResponse($functionName, $payload);
	return $results;
}

/**
  * Displays the details for a video.
  * @param string $video_id
  * @return string $results
  */
function videos_get_details($video_id)
{
	$functionName = "youtube.videos.get_details";
	$payload = array("video_id"=>$video_id);;
	$results = $this->getAPIResponse($functionName, $payload);
	return $results;
}

/**
  * Lists all videos that have the specified tag.
  * @param string $tag
  * @param string $page
  * @param string $per_page
  * @return string $results
  */
function videos_list_by_tag($tag, $page, $per_page)
{
	$functionName = "youtube.videos.list_by_tag";
	$payload = array("tag"=>$tag,"page"=>$page,"per_page"=>$per_page);
	$results = $this->getAPIResponse($functionName, $payload);
	return $results;
}

/**
  * Lists all videos that were uploaded by the specified user
  * @param string $user
  * @return string $results
  */
function videos_list_by_user($user)
{
	$functionName = "youtube.videos.list_by_user";
	$payload = array("user"=>$user);
	$results = $this->getAPIResponse($functionName, $payload);
	return $results;
}

/**
  * Lists the most recent 25 videos that have been featured on the 
  * front page of the YouTube site.
  * @return string $results
  */
function videos_list_featured()
{
	$functionName = "youtube.videos.list_featured";
	$payload = "";
	$results = $this->getAPIResponse($functionName, $payload);
	return $results;
}

/**
  * A helper wrapper for the videos.list_by_tag. 
  * @param string $tags
  * @param string $tags
  * @return array $videos
  */
function getVideosByTag($tags= '', $numvids = 10){
	str_replace(" ","+",$tags);
	$videosReponse = $this->videos_list_by_tag($tags, 1, $numvids);
	$videos = $this->videoListHelper($videosReponse);
	return $videos;
}

/**
  * Helps get a plain video list from the simpleXML object
  * @param string $xmlArray
  * @return string $videoList
  */
function videoListHelper($xmlArray)
{
	$videoList =  $xmlArray->video_list;
	$videos = $videoList->video;
	return $videos;
}

/**
  * Get a rest response from the youtube api. Takes a functionName.
  * and a array payload
  * @param string $functionName
  * @param string $payload
  * @return string $response
  */
function getAPIResponse($functionName, $payload)
{
	$url = $this->buildRestUrl($functionName, $payload);
	$response_obj = $this->doRestRequest($url);
	$response_array = array();
	$this->obj2array($response_obj, $response_array);
	return $response_obj;
}

/**
  * Does a REST request based on a specific url
  * @param string $request_url
  * @return string $xmlResults
  */
function doRestRequest($request_url)
{
	$results = file_get_contents($request_url);
	$xmlResults = $this->resultXML($results);
	return $xmlResults;
}

/**
  * Turns the XML results into an simpleXML array
  * @param string $results
  * @return string $xml
  */
function resultXML($results)
{
	$xml_array = $xml = new SimpleXMLElement($results);
	return $xml_array;
}

/**
  * Builds REST URL based on  payload and function
  * @param string $functionName
  * @param array $payload
  * @return string $url
  */
function buildRestUrl($functionName, $payload)
{
	if ($payload != ""){
		foreach ($payload as $name => $value){
			$payloadString .= $name.'='.$value.'&';
		}
	}
	$url = 'http://www.youtube.com/api2_rest?method='.$functionName.'&dev_id='.$this->devkey.'&'.$payloadString;
	return $url;
}

function obj2array($obj, &$subject_array=array()) {
   foreach ((array) $obj as $key => $var) {
       if (is_object($var)) {
           if(count((array) $var) == 0) {
               $subject_array[$key] = 'NULL';
           }
           else {
               $this->obj2array($var, $subject_array[$key]);
           }
       }
       else {
           $subject_array[$key] = $var;
       }
   }
}

//http://us2.php.net/manual/en/ref.simplexml.php#68286
function simplexml2ISOarray($xml) {
   if (get_class($xml) == 'SimpleXMLElement') {
       $attributes = $xml->attributes();
       foreach($attributes as $k=>$v) {
           if ($v) $a[$k] = (string) $v;
       }
       $x = $xml;
       $xml = get_object_vars($xml);
   }
   if (is_array($xml)) {
       if (count($xml) == 0) return (string) $x; // for CDATA
       foreach($xml as $key=>$value) {
           if($key != '@attributes') {
               $r[$key] = simplexml2ISOarray($value);
               if ( !is_array( $r[$key] ) ) $r[$key] = utf8_decode( $r[$key] );
           }
       }
       if (isset($a)) $r['@'] = $a; // Attributes
       return $r;
   }
   return (string) $xml;
}

function getVideosFromList($video_list)
{
	$videos["videos"]=$video_list->video_list->video;
	$videos["count"] = count($video_list->video_list->video);
	return $videos;
}



}
